/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.sql.Connection;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import kitaplaner.database.Database;
import kitaplaner.database.Kind;
import kitaplaner.database.Kita;

/**
 *
 * @author kristina
 */
public class KindApp {
    Kita kita = new Kita();
    Database db = new Database();
    Connection conn;

    public KindApp (Kita kita, Connection conn)
    {
        this.kita = kita;
        this.conn = conn;
    }

    public Kind getKindById(int id)
    {
        Kind kind = null;

        ArrayList <Kind> alKind = new ArrayList<Kind> ();

        for (int i = 0; i < alKind.size(); i++)
        {
            if (alKind.get(i).getId() == id)
            {
                kind = alKind.get(i);
            }
        }

        return kind;
    }

    public Boolean deleteChild(int id)
    {
        ArrayList <Kind> alKind = kita.getKind();
        Boolean success = false;

        for (int i = 0 ; i < alKind.size() ; i++)
        {
            if (alKind.get(i).getId() == id)
            {
                alKind.remove(i);
            }
        }

        success = db.deleteKind(conn, id);

        kita.setKind(alKind);

        return success;
    }

    public Kind getKindByName(String prename, String lastname)
    {
        Kind kind = null;

        ArrayList <Kind> alKind = kita.getKind();

        for (int i = 0 ; i < alKind.size() ; i++)
        {
            if (alKind.get(i).getVorname().equals(prename)
             && alKind.get(i).getNachname().equals(lastname))
            {
                kind = alKind.get(i);
            }
        }

        return kind;
    }

    public Object [] createCompoboxText()
    {
        Object [] comboItems;

        ArrayList <Kind> kind = kita.getKind();
        ArrayList <String> kindName = new ArrayList<String>();

        for (int i = 0; i < kind.size(); i++)
        {
            kindName.add(kind.get(i).getVorname()+" "+kind.get(i).getNachname());
        }
        comboItems = kindName.toArray();

        return comboItems;
    }

    public DefaultTableModel createKindOverview()
    {
        GruppenApp grpApp = new GruppenApp(this.kita, conn);
        String[] columnNames = {"ID", "Vorname", "Nachname", "Straße",
                                "Hausnummer", "PLZ", "Stadt", "Telefon",
                                "Email", "Geburtsdatum",
                                "Allergien", "SÖZ Start",
                                "SÖZ Ende", "Gruppenname"};

        ArrayList <Kind> kindList = kita.getKind();
        DefaultTableModel model = new DefaultTableModel();
        ArrayList <Kind> alKind = new ArrayList <Kind>();
        JTable table = new JTable(model);

        //insert Columns
        for (int i = 0 ; i < columnNames.length; i++)
        {
            model.addColumn(columnNames[i]);
        }

        for (int i = 0; i < kindList.size(); i++)
        {
            int grpId = kindList.get(i).getGrpID();

            Object []obj = {kindList.get(i).getId(),
                            kindList.get(i).getVorname(),
                            kindList.get(i).getNachname(),
                            kindList.get(i).getStraße(),
                            kindList.get(i).getHausnr(),
                            kindList.get(i).getPlz(),
                            kindList.get(i).getStadt(),
                            kindList.get(i).getTelefon(),
                            kindList.get(i).getEmail(),
                            kindList.get(i).getGebDat(),
                            kindList.get(i).getAllergie(),
                            kindList.get(i).getSöz_start(),
                            kindList.get(i).getSöz_end(),
                            grpApp.getGrpNameById(grpId)};
            model.insertRow (model.getRowCount(), obj);
        }
        return model;
    }

    public Boolean insertIntoKind(String vorname, String nachname, String straße,
            String hnr, int plz, String stadt, String telefon, String email,
            int grpId, String allergien, Date gebDat, Time soz_start,
            Time soz_end)
    {
        Boolean success = false;

        Kind kind = new Kind();
        kind.setEmail(email);
        kind.setGebDat(gebDat);
        kind.setGrpID(grpId);
        kind.setHausnr(hnr);
        kind.setNachname(nachname);
        kind.setVorname(vorname);
        kind.setPlz(plz);
        kind.setStadt(stadt);
        kind.setStraße(straße);
        kind.setTelefon(telefon);
        kind.setSöz_start(soz_start);
        kind.setSöz_end(soz_end);
        kind.setAllergie(allergien);

        conn = db.checkConnection(conn);
        success = db.insertKind(conn, kind);
        conn = db.checkConnection(conn);
        kita.setKind(db.getKinder(conn));

        return success;
    }

    public Boolean changeKind(int id, String vorname, String nachname, String straße,
            String hnr, int plz, String stadt, String telefon, String email,
            int grpId, String allergien, Date gebDat, Time soz_start,
            Time soz_end)
    {
        Boolean success = false;
        ArrayList <Kind> alKind = kita.getKind();

        for (int i = 0; i < alKind.size(); i++)
        {
            if (alKind.get(i).getId() == id)
            {
                alKind.get(i).setEmail(email);
                alKind.get(i).setGebDat(gebDat);
                alKind.get(i).setGrpID(grpId);
                alKind.get(i).setHausnr(hnr);
                alKind.get(i).setNachname(nachname);
                alKind.get(i).setVorname(vorname);
                alKind.get(i).setPlz(plz);
                alKind.get(i).setStadt(stadt);
                alKind.get(i).setStraße(straße);
                alKind.get(i).setTelefon(telefon);
                alKind.get(i).setSöz_start(soz_start);
                alKind.get(i).setSöz_end(soz_end);
                alKind.get(i).setAllergie(allergien);
                conn = db.checkConnection(conn);
                success = db.changeKind(conn, alKind.get(i));
                conn = db.checkConnection(conn);
                kita.setKind(db.getKinder(conn));
            }
        }
        return success;
    }

    public TableModel createKindOverviewByGroup(String gruppenName) {
           
        GruppenApp grpApp = new GruppenApp(this.kita, conn);
        String[] columnNames = {"ID", "Vorname", "Nachname", "Straße",
                                "Hausnummer", "PLZ", "Stadt", "Telefon",
                                "Email", "Geburtsdatum",
                                "Allergien", "SÖZ Start",
                                "SÖZ Ende", "Gruppenname"};

        ArrayList<Kind> kindList = kita.getKind();
        DefaultTableModel model = new DefaultTableModel();
        ArrayList<Kind> alKind = new ArrayList<Kind>();
        JTable table = new JTable(model);

        //insert Columns
        for (int i = 0; i < columnNames.length; i++) {
            model.addColumn(columnNames[i]);
        }

        for (int i = 0; i < kindList.size(); i++) {
            int grpID = kindList.get(i).getGrpID();
            
            if (grpApp.getGrpNameById(grpID).equals(gruppenName)) {
                
                Object[] obj = {kindList.get(i).getId(),
                                kindList.get(i).getVorname(),
                                kindList.get(i).getNachname(),
                                kindList.get(i).getStraße(),
                                kindList.get(i).getHausnr(),
                                kindList.get(i).getPlz(),
                                kindList.get(i).getStadt(),
                                kindList.get(i).getTelefon(),
                                kindList.get(i).getEmail(),
                                kindList.get(i).getGebDat(),
                                kindList.get(i).getAllergie(),
                                kindList.get(i).getSöz_start(),
                                kindList.get(i).getSöz_end(),
                                grpApp.getGrpNameById(grpID)};
                
                model.insertRow(model.getRowCount(), obj);
            }
        }
        return model;
    }
}
